library flutter_tag;

import 'package:flutter/material.dart';

typedef OnPressedCallback = void Function(String i);

class TagList extends StatefulWidget {
  final Widget Function(int index) itemBuilder;
  final Widget Function()? addTagBuilder;
  final double runSpacing;
  final double spacing;
  final int tagCount;

  const TagList({
    super.key,
    required this.itemBuilder,
    this.addTagBuilder,
    this.runSpacing = 10.0,
    this.spacing = 10.0,
    this.tagCount = 0,
  });

  @override
  TagListState createState() => TagListState();
}

class TagListState extends State<TagList> {
  @override
  Widget build(BuildContext context) {
    return Wrap(
        runSpacing: widget.runSpacing,
        spacing: widget.spacing,
        children: [
          ...List.generate(widget.tagCount, (index) {
            return widget.itemBuilder(index);
          }),
          widget.addTagBuilder != null ? widget.addTagBuilder!() : Container(),
        ]);
  }
}

//默认样式，当传入tags字段时，才会触发
class TagItem extends StatelessWidget {
  final Widget? leadingIcon;
  final Widget? traillingIcon;
  final String tag;
  final TextStyle? textStyle;
  final TextStyle? activeTextStyle;
  final Color? color; //背景色
  final Color? activeColor; //
  final bool active;
  final bool canActive;
  final EdgeInsets? padding;
  final OnPressedCallback? onPressed;
  final Widget Function(String tag)? builder;
  final double? height;
  final double? borderRadius;
  final Border? border;

  const TagItem({
    Key? key,
    required this.tag,
    this.leadingIcon,
    this.traillingIcon,
    this.textStyle = const TextStyle(fontSize: 12.0, color: Colors.white),
    this.activeTextStyle = const TextStyle(fontSize: 12.0, color: Colors.white),
    this.color = Colors.black,
    this.activeColor = Colors.red,
    this.active = false,
    this.canActive = false,
    this.padding = const EdgeInsets.all(5.0),
    this.onPressed,
    this.builder,
    this.height = 30,
    this.borderRadius,
    this.border,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisSize: MainAxisSize.min,
      children: [
        builder != null
            ? builder!(tag)
            : GestureDetector(
                onTap: () {
                  if (canActive) {
                    onPressed?.call(tag);
                  }
                },
                child: Container(
                    height: height,
                    padding: padding,
                    alignment: Alignment.center,
                    decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(
                            borderRadius ?? height! / 2.0),
                        color: active ? activeColor : color,
                        border: border ??
                            Border.all(
                                width: 1,
                                color: active ? color! : activeColor!)),
                    child: Row(
                      children: [
                        leadingIcon ?? Container(),
                        Text(
                          tag,
                          style: (active ? activeTextStyle : textStyle),
                        ),
                        traillingIcon ?? Container(),
                      ],
                    )),
              ),
      ],
    );
  }
}
